package vip.zcn.mrn.service.impl;

import org.springframework.stereotype.Service;
import vip.zcn.mrn.model.LogEntry;
import vip.zcn.mrn.service.LogParserService;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
public class LogParserServiceImpl implements LogParserService {
    private static final Pattern LOG_PATTERN = Pattern.compile("\\[(.*?)\\] \\s*(\\w+) \\[(.*?)\\] \\s*(\\S+)#(\\S+) \\s*(.*)");

    @Override
    public LogEntry parseLog(String logLine) {
        Matcher matcher = LOG_PATTERN.matcher(logLine);
        if (matcher.find()) {
            LogEntry entry = new LogEntry();
            entry.setTime(matcher.group(1));
            entry.setLevel(matcher.group(2));
            entry.setThread(matcher.group(3));
            entry.setClassName(matcher.group(4));
            entry.setMethodName(matcher.group(5));
            entry.setMessage(matcher.group(6));
            return entry;
        }
        return null;
    }
}
